// ntripClient.cc
#include "ntripClient.h"
#include "src/rtklib.h"
#include <string>

namespace demo {

using namespace v8;
using namespace std;

Persistent<Function> NtripClient::constructor;

NtripClient::NtripClient(char* mountpoint, char*  ip, char*  port, char*  username, char*  password, char*  socketIp, char*  socketPort){
    startClient(mountpoint, ip, port, username, password, socketIp, socketPort);
}

NtripClient::~NtripClient() {
}

void NtripClient::Init(Local<Object> exports) {
    Isolate* isolate = exports->GetIsolate();

    Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
    tpl->SetClassName(String::NewFromUtf8(isolate, "NtripClient"));
    tpl->InstanceTemplate()->SetInternalFieldCount(1);

    // 原型
    NODE_SET_PROTOTYPE_METHOD(tpl, "stop", stop);

    constructor.Reset(isolate, tpl->GetFunction());
    exports->Set(String::NewFromUtf8(isolate, "NtripClient"),
               tpl->GetFunction());
}

void NtripClient::New(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();
    if (args.Length() < 7) {
        isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Number of parameters must be 7.")));
        return;
    }
    if (!args[0]->IsString()||!args[1]->IsString()||!args[2]->IsString()||!args[3]->IsString()
    ||!args[4]->IsString()||!args[5]->IsString()||!args[6]->IsString()) {
        isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "All parameters must be String.")));
        return;
    }

    v8::String::Utf8Value param1(args[0]->ToString());
    string s_mount = string(*param1);
    v8::String::Utf8Value param2(args[1]->ToString());
    string s_ip = string(*param2);
    v8::String::Utf8Value param3(args[2]->ToString());
    string s_port = string(*param3);
    v8::String::Utf8Value param4(args[3]->ToString());
    string s_uname = string(*param4);
    v8::String::Utf8Value param5(args[4]->ToString());
    string s_pword = string(*param5);
    v8::String::Utf8Value param6(args[5]->ToString());
    string s_socketIp = string(*param6);
    v8::String::Utf8Value param7(args[6]->ToString());
    string s_socketPort = string(*param7);

    if (args.IsConstructCall()) {
    // new NtripClient(...)
        NtripClient* obj = new NtripClient((char*)s_mount.data(),(char*)s_ip.data(),
                (char*)s_port.data(),(char*)s_uname.data(),(char*)s_pword.data(),
                (char*)s_socketIp.data(),(char*)s_socketPort.data());
        obj->Wrap(args.This());
        args.GetReturnValue().Set(args.This());
    } else {
    // NtripClient(...)
    const int argc = 1;
    Local<Value> argv[argc] = { args[0] };
    Local<Context> context = isolate->GetCurrentContext();
    Local<Function> cons = Local<Function>::New(isolate, constructor);
    Local<Object> result =
        cons->NewInstance(context, argc, argv).ToLocalChecked();
    args.GetReturnValue().Set(result);
    }
}

void NtripClient::stop(const FunctionCallbackInfo<Value>& args) {
  
  Isolate* isolate = args.GetIsolate();
  stopClient();
  args.GetReturnValue().Set(Number::New(isolate, 0));
}

}  // namespace demo